package com.kuyou.publiclibrary.utils;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.Log;

import java.util.List;

public class ECGBitmapUtils {
    private Bitmap bitmap;
    private int Glide_length = 20;
    private Paint Glide_paint;
    private int baseLine = Glide_length * 15;
    //绘制网格线粗线的画笔
    private Paint Glide_bold_paint;
    private Paint EGC_paint;
    private Canvas canvas;
    private List<Integer> list;
    private float modifyY;

    public ECGBitmapUtils() {
        Glide_paint = new Paint();
        Glide_paint.setStrokeJoin(Paint.Join.ROUND);
        Glide_paint.setStrokeCap(Paint.Cap.ROUND);
        Glide_paint.setStrokeWidth(2);
        Glide_paint.setColor(Color.RED);
        Glide_paint.setPathEffect(new DashPathEffect(new float[]{6, 3}, 0));
        Glide_bold_paint = new Paint();
        Glide_bold_paint.setStrokeJoin(Paint.Join.ROUND);
        Glide_bold_paint.setStrokeCap(Paint.Cap.ROUND);
        Glide_bold_paint.setStrokeWidth(3);
        Glide_bold_paint.setColor(Color.RED);
        Glide_bold_paint.setTextSize(20);

        EGC_paint = new Paint();
        EGC_paint.setStrokeJoin(Paint.Join.ROUND);
        EGC_paint.setStrokeCap(Paint.Cap.ROUND);
        EGC_paint.setStyle(Paint.Style.STROKE);
        EGC_paint.setStrokeWidth(6);
        EGC_paint.setColor(Color.BLUE);
        modifyY = 100 / Glide_length;

    }


    public Canvas getCanvas() {
        return canvas;
    }

    public void setList(List<Integer> list) {
        this.list = list;
        bitmap = Bitmap.createBitmap(
                list.size()+40,
                Glide_length *35,
                Bitmap.Config.RGB_565);

        canvas = new Canvas(bitmap);
        draw();
    }

    public List<Integer> getList() {
        return list;
    }

    public void setCanvas(Canvas canvas) {
        this.canvas = canvas;
    }

    public void draw() {
        canvas.drawColor(Color.WHITE);
        drawGlide(canvas, Glide_paint, Glide_bold_paint);
        drawEGCLine(canvas, EGC_paint, list);
    }

    private void drawGlide(Canvas canvas, Paint paint, Paint paint_bold) {
        double time = 0.04;
        int vertz = 0;
        int hortz = 0;
        int lineStyle = 1;
        for (int i = 0; i < list.size() / Glide_length; i++) {
            if (lineStyle == 1) {
                if (i <= 30) {
                    canvas.drawLine(0, vertz, list.size(), vertz, paint_bold);
                 }
                canvas.drawLine((float) hortz, (float) 0, (float) hortz, (float) Glide_length * 30, paint_bold);
                canvas.drawText(String .format("%.2f秒",time*i),hortz,Glide_length * 32,paint_bold);
                vertz += Glide_length;
                hortz += Glide_length;
            } else {
                if (i <= 30) {
                    canvas.drawLine((float) 0, (float) vertz, (float) list.size(), vertz, paint);
                }
                canvas.drawLine((float) hortz, (float) 0, (float) hortz, (float) Glide_length * 30, paint);
                vertz += Glide_length;
                hortz += Glide_length;
            }
            lineStyle++;
            if (lineStyle == 6) {
                lineStyle = 1;
            }
        }
        canvas.drawLine((float) hortz, (float) 0, (float) hortz, (float) Glide_length * 30, paint_bold);
        canvas.drawText(String .format("30秒"),hortz,Glide_length * 32,paint_bold);

    }

    public Bitmap getBitmap() {
        return bitmap;
    }

    public void setBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;
    }

    private void drawEGCLine(Canvas canvas, Paint paint, List<Integer> list_data) {

        int endX = 0;
        Path path = new Path();
        path.moveTo(0, baseLine);
        for (float heart_data : list_data) {
            Log.d("ECGUtils", (baseLine - (heart_data* 18.3) / 128.0 / modifyY)+"");

            path.lineTo((float) endX, (float) (baseLine - (heart_data* 18.3) / 128.0 / modifyY));
            endX = endX + 1;
        }

        canvas.drawPath(path, paint);

    }
}
